package uncommonFromSentences;

import java.util.*;

public class Solution {
    public String[] uncommonFromSentences(String s1, String s2) {
        Set<String>[] set1 = get(s1);
        Set<String>[] set2 = get(s2);
//        System.out.println(set1);
//        System.out.println(set2);
        List<String> ans = new ArrayList<>();
        for (String s: set1[0]) {
            if(!set2[1].contains(s)) {
                ans.add(s);
            }
        }
        for (String s: set2[0]) {
            if(!set1[1].contains(s)) {
                ans.add(s);
            }
        }
        return ans.toArray(new String[ans.size()]);
    }
    private Set<String>[] get(String s) {
        Set<String> set = new HashSet<>();
        Set<String> ans = new HashSet<>();
        int i = 0;
        StringBuilder sb = new StringBuilder();
        while (i<s.length()) {
            if(i == s.length()-1) {
                sb.append(s.charAt(i));
                if(set.contains(sb.toString())) {
                    ans.remove(sb.toString());
                } else {
                    ans.add(sb.toString());
                    set.add(sb.toString());
                }
                i++;
            } else if(s.charAt(i)!=' ') {
                sb.append(s.charAt(i));
                i++;
            } else {
                if(set.contains(sb.toString())) {
                    ans.remove(sb.toString());
                } else {
                    set.add(sb.toString());
                    ans.add(sb.toString());
                }
                sb = new StringBuilder();
                i++;
            }
        }
        return new Set[]{ans,set};
    }
}
